package com.cityos.android.mystudynestedscroll.ui.behavior

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewParent
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.ViewCompat
import com.cityos.android.mystudynestedscroll.R
import com.cityos.android.mystudynestedscroll.common.utils.DisplayUtils
import com.cityos.android.mystudynestedscroll.common.utils.LoggerUtils
import com.youth.banner.Banner

/**
 * @author hailong
 * @date 2024年03月04日 16:54
 *
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
class UcBannerBehavior(context: Context? , attrs: AttributeSet?) : CoordinatorLayout.Behavior<Banner<* , *>>(context , attrs) {

    private val scrollMax = DisplayUtils.dp2px(context , 100f)
    private var dependencyView: View? = null
    private val avg = 2f


    override fun onStartNestedScroll(coordinatorLayout: CoordinatorLayout , child: Banner<* , *> , directTargetChild: View , target: View , axes: Int , type: Int): Boolean {
        if (dependencyView == null) {
            dependencyView = coordinatorLayout.findViewById(R.id.uc_home_viewpage)
        }
        return axes == ViewCompat.SCROLL_AXIS_VERTICAL
    }


    override fun onNestedPreScroll(coordinatorLayout: CoordinatorLayout , child: Banner<* , *> , target: View , dx: Int , dy: Int , consumed: IntArray , type: Int) {
        if (dependencyView == null) {
            val tempDy = (dy / avg).toInt()
            if (tempDy > 0) {
                if (child.translationY <= -scrollMax) {
                    consumed[1] = 0
                    child.translationY = -scrollMax.toFloat()
                } else {
                    if (child.top - tempDy < -scrollMax) {
                        val deltaY = scrollMax - Math.abs(child.top)
                        child.translationY = -scrollMax.toFloat()
                        consumed[1] = deltaY
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            } else {
                if (child.translationY >= 0) {
                    consumed[1] = 0
                    child.translationY = 0f
                } else {
                    if (child.translationY - tempDy > 0) {
                        child.translationY = 0f
                        consumed[1] = child.translationY.toInt()
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            }
        } else {

            val tempDy = (dy / avg).toInt()
            if (tempDy > 0) {
                if (child.translationY <= -scrollMax) {
                    consumed[1] = 0
                    child.translationY = -scrollMax.toFloat()
                } else {
                    if (child.top - tempDy < -scrollMax) {
                        val deltaY = scrollMax - Math.abs(child.top)
                        child.translationY = -scrollMax.toFloat()
                        consumed[1] = deltaY
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            } else {
                if (dependencyView!!.translationY < 0) {
                    consumed[1] = 0
                    return
                }

                if (child.translationY >= 0) {
                    consumed[1] = 0
                    child.translationY = 0f
                } else {
                    if (child.translationY - tempDy > 0) {
                        child.translationY = 0f
                        consumed[1] = child.translationY.toInt()
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            }
        }
    }


    override fun onNestedScroll(coordinatorLayout: CoordinatorLayout , child: Banner<* , *> , target: View , dxConsumed: Int , dyConsumed: Int , dxUnconsumed: Int , dyUnconsumed: Int , type: Int , consumed: IntArray) {
        if (dyUnconsumed == 0) {
            return
        }
        if (dependencyView == null) {
            val tempDy = (dyUnconsumed / avg).toInt()
            if (tempDy > 0) {
                if (child.translationY <= -scrollMax) {
                    consumed[1] = 0
                    child.translationY = -scrollMax.toFloat()
                } else {
                    if (child.top - tempDy < -scrollMax) {
                        val deltaY = scrollMax - Math.abs(child.top)
                        child.translationY = -scrollMax.toFloat()
                        consumed[1] = deltaY
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            } else {
                if (child.translationY >= 0) {
                    consumed[1] = 0
                    child.translationY = 0f
                } else {
                    if (child.translationY - tempDy > 0) {
                        child.translationY = 0f
                        consumed[1] = child.translationY.toInt()
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            }
        } else {
            val tempDy = (dyUnconsumed / avg).toInt()
            if (tempDy > 0) {
                if (child.translationY <= -scrollMax) {
                    consumed[1] = 0
                    child.translationY = -scrollMax.toFloat()
                } else {
                    if (child.top - tempDy < -scrollMax) {
                        val deltaY = scrollMax - Math.abs(child.top)
                        child.translationY = -scrollMax.toFloat()
                        consumed[1] = deltaY
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            } else {
                if (child.translationY >= 0) {
                    consumed[1] = 0
                    child.translationY = 0f
                } else {
                    if (child.translationY - tempDy > 0) {
                        child.translationY = 0f
                        consumed[1] = child.translationY.toInt()
                    } else {
                        child.translationY = child.translationY - tempDy.toFloat()
                        consumed[1] = tempDy
                    }
                }
            }
        }

    }

}